package com.mmp.main.servlet.systemsettings;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mmp.common.bo.Message;
import com.mmp.common.servlet.BasicServlet;
import com.mmp.main.endpoints.JobOrderNotificationsEndPoint;
import com.mmp.main.entity.User;

public class ChangePasswordProcessServlet extends BasicServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		User sessionUser = getSessionUser(request);

		String inputCurrentPass = request.getParameter("currentPass");

		if (inputCurrentPass != null && !inputCurrentPass.isEmpty()) {
			if (!inputCurrentPass.equals(sessionUser.getPassword())) {
				setMessage(request, new Message("كلمة المرور  التي تم إدخالها غير صحيحة", Message.MESSAGE_TYPE.ERROR));
				sendRedirect(request, response, "ChangePasswordDisplay");
				return;

			} else {
				String newPass = request.getParameter("newPass");
				String confNewPass = request.getParameter("confNewPass");

				if (newPass != null && !newPass.isEmpty() && confNewPass != null && !confNewPass.isEmpty())
					if (newPass.equals(confNewPass)) {

						sessionUser.setPassword(newPass);
						dbm.mergeEntity(sessionUser);
			
						setMessage(request, new Message("تم تغيير كلمة المرور بنجاح", Message.MESSAGE_TYPE.SUCCESS));
						sendRedirect(request, response, "Logout");
						return;
					} else {
						setMessage(request, new Message("تأكد من كلمة المرور الجديدة", Message.MESSAGE_TYPE.ERROR));
						sendRedirect(request, response, "ChangePasswordDisplay");
						return;
					}
			}
		} else {
			setMessage(request, new Message("من فضلك ادخل كلمة المرور الحالية", Message.MESSAGE_TYPE.ERROR));
			sendRedirect(request, response, "ChangePasswordDisplay");
			return;
		}

	}
}
